<?php
/**
 * user
 * @version 1.0
 * @package application
 * @subpackage application/controllers/user/
 */
class User extends MY_Controller
{	
	public function __construct(){
		parent::__construct();
		$this->_rule_config=array(array('field'=> 'user_id' , 'label'=> 'user_id' , 'rules'=> 'required'),
			array('field'=> 'email' , 'label'=> '邮箱' , 'rules'=> 'required'),
			array('field'=> 'user_name' , 'label'=> '用户名' , 'rules'=> 'required'),
			array('field'=> 'password' , 'label'=> '密码' , 'rules'=> 'required'),
			array('field'=> 'name' , 'label'=> '姓名' , 'rules'=> 'required'),
			array('field'=> 'avatar' , 'label'=> '头像' , 'rules'=> 'required'),
			array('field'=> 'user_rank' , 'label'=> '会员等级' , 'rules'=> 'required'),
			array('field'=> 'phone' , 'label'=> '电话' , 'rules'=> 'required'),
			array('field'=> 'address' , 'label'=> '地址' , 'rules'=> 'required'),
			array('field'=> 'country_id' , 'label'=> '国家' , 'rules'=> 'required'),
			array('field'=> 'city_id' , 'label'=> '城市' , 'rules'=> 'required'),
			array('field'=> 'city' , 'label'=> '城市' , 'rules'=> 'required'),
			array('field'=> 'province_id' , 'label'=> '省份' , 'rules'=> 'required'),
			array('field'=> 'province' , 'label'=> '省份' , 'rules'=> 'required'),
			array('field'=> 'postcode' , 'label'=> '邮编' , 'rules'=> 'required'),
			array('field'=> 'qq' , 'label'=> 'QQ' , 'rules'=> 'required'),
			array('field'=> 'created' , 'label'=> '创建日期' , 'rules'=> 'required'),
			array('field'=> 'modified' , 'label'=> '修改日期' , 'rules'=> 'required'),
			array('field'=> 'status' , 'label'=> '状态' , 'rules'=> 'required'),
			);

		$this->load->model('user_model');
	}
	/**
	 * index 显示列表页
	 * @author 
	 */	
	public function index(){
		$attributes=$this->input->get();
		$search['attributes']=$attributes;
		// 总数与分页
		$total = $this->user_model->total($search);
		$data['total'] = $total;
		// 请使用config_item( 'per_page' )获取全局显示条数
		$per_page = config_item( 'per_page' );
		$this->load->library('pagination');
		$pagination_config = array(
			'base_url' => base_url($this->_site_path.'/user/index' ),
			'total_rows' => $total,
			'per_page' => $per_page,
			'uri_segment' => 4,
		);
		$this->pagination->initialize( $pagination_config );
		$data['pagination'] = $this->pagination->create_links();

		$search['limit']=array('persize'=>$per_page,'offset'=>$this->pagination->get_cur_offset());
		$data['user_data']=$this->user_model->all($search);
		$this->load->view($this->_site_path.'/user/index',$data);
	}

	/**
	 * view 显示详细页
	 * @param $id 主键值
	 * @author 
	 */
	public function view($id)
	{
		$data['user_data']=$this->user_model->get_by_pk($id);
		$this->load->view($this->_site_path.'/user/view',$data);
	}

	/**
	 * add 添加 
	 * @author 
	 */
	public function add()
	{
		$data = array( );

		if ( $this->input->post( 'is_submit' ) && $this->form_validation->check_token() ){
			try {
				$user_data = $this->input->post();
				$data['user_data'] = $user_data;
				//设置验证规则
				$this->form_validation->set_rules($this->_rule_config);
				if ( !$this->form_validation->run() ) throw new Exception( validation_errors(), 0 );
				//插入
				$user_id = $this->user_model->insert( $user_data );
				init_messagebox( '添加成功', 'success', 1, base_url( $this->_site_path.'/user/index' ) );
			} catch ( Exception $e ) {
				init_messagebox( $e->getMessage(), 'error', $e->getCode() );
			}
		}
		$this->load->view($this->_site_path.'/user/form', $data );
	}

	/**
	 * edit 编辑 
	 * @param $id 主键值
	 * @author 
	 */
	public function edit($id)
	{
		$data = array( );
		$data['user_data']=$this->user_model->get_by_pk($id);
		if ( $this->input->post( 'is_submit' ) && $this->form_validation->check_token() ){
			try {
				$user_data = $this->input->post();
				$data['user_data'] = $user_data;
				//设置验证规则
				$this->form_validation->set_rules($this->_rule_config);
				if ( !$this->form_validation->run() ) throw new Exception( validation_errors(), 0 );
				//修改
				$this->user_model->update_by_pk( $user_data ,$id);
				init_messagebox( '修改成功', 'success', 1, base_url( $this->_site_path.'/user/index' ) );
			} catch ( Exception $e ) {
				init_messagebox( $e->getMessage(), 'error', $e->getCode() );
			}
		}
		$this->load->view($this->_site_path.'/user/form', $data );
	}	

	/**
	 * delete 删除
	 * @param $id 主键值
	 * @author 
	 */
	public function delete( $id ) {
		$this->user_model->delete_by_pk( $id );
		redirect( base_url($this->_site_path.'/user/index' ) );
	}

	/**
	 * delete_all 批量删除
	 * @param $id 主键值
	 * @author 
	 */
	public function delete_all() {
		$ids=$this->input->post('ids');
		foreach($ids as $id){
			$this->user_model->delete_by_pk( $id );
		}
		redirect( base_url($this->_site_path.'/user/index' ) );
	}
}
